#include "admin.h"

//管理员界面
int adminFace()
{
    while(1)
    {
        system("clear");
        printf("请选择需要的功能:\n");
        printf("1.发行彩票\n");
        printf("2.查询彩民信息\n");
        printf("3.排序\n");
        printf("4.保存\n");
        printf("0.返回登录界面\n");
        int iCho=9;
        if(1 != scanf("%d",&iCho))
        {
            chidaodi;
        }
        else
        {
            getchar();
        }
        if(iCho!=1&&iCho!=2&&iCho!=3&&iCho!=4&&iCho!=0)
        {
            printf("选择错误！两秒后请重新选择!\n");
            sleep(2);
            continue;
        }
        return iCho;
    }
}

//管理员选择界面
void switchByAdmin()
{
    int ret = adminFace();
    switch (ret)
    {
    case 1:
        issueLottery();
        switchByAdmin();
        break;
    case 2:
        switchBySearchFace();
        switchByAdmin();
        break;
    case 3:
        switchBySortWhatFace();
        switchByAdmin();
        break;
    case 4:
        printf("保存成功!");
        huichejixu();
        switchByAdmin();
        break;
    case 0:
        loginSwitch();
        break;
    default:
        break;
    }
}

//查询彩民信息选择界面
int searchUsrInfoFace()
{
    while(1)
    {
        system("clear");
        printf("请选择查询的依据:\n");
        printf("1.用户名\n");
        printf("2.余额\n");
        printf("0.不想查了\n");
        int iCho=9;
        if(1 != scanf("%d",&iCho))
        {
            chidaodi;
        }
        else
        {
            getchar();
        }
        if(iCho!=1&&iCho!=2&&iCho!=0)
        {
            printf("选择错误！两秒后请重新选择!\n");
            sleep(2);
            continue;
        }
        return iCho;
    }
}

//根据查询彩民信息选择进入界面
void switchBySearchFace()
{
    int ret = searchUsrInfoFace();
    switch (ret)
    {
    case 1:
        searchByUsrName();
        break;
    case 2:
        searchByUsrMoney();
        break;
    case 0:
        //退出
        break;
    default:
        break;
    }
}

//保存
void baocun()
{
    printf("保存成功！");
    //qingping;
    printf("保存成功！");
    sleep(2);
}

//根据名字查询chengg民信息
void searchByUsrName()
{
    usrList *list=mkList();
    usrNode *node=NULL;
    loadInfoFromFile(PATH_NAME,list);
    char playerName[NAME_LEN]={'\0'};
    while(1)
    {
        system("clear");
        printf("请输入想要查询的彩民用户名：\n");
        getSTR(playerName,NAME_LEN);
        node=findNodeByName(list,playerName);
        if(node == NULL)
        {
            printf("发生错误！-->该用户不存在！\n");
            printf("请一秒后重新输入！\n");
            sleep(1);
            continue;
        }
        break;
    }
    playerDisplay(node);
    releaseUsrList(NULL,list);
}

//根据余额查询彩民信息
void searchByUsrMoney()
{
    double money_up=0,money_down=0;
    while(1)
    {
        system("clear");
        printf("请输入余额区间下限:\n");
        scanf("%lf",&money_down);
        printf("请输入余额区间上限:\n");
        scanf("%lf",&money_up);
        chidaodi;
        if(money_down<0 || money_up<0 ||money_down>money_up)
        {
            printf("您输入的区间有误，两秒后请重新输入!");
            sleep(2);
            continue;
        }
        break;
    }
    usrList *list=mkList();
    loadInfoFromFile(PATH_NAME,list);
    usrNode *node=list->pFirstNode;
    printf("用户名\t密码\t余额\n");
    while(node != NULL)
    {
        if(node->usrMoney <= money_up && node->usrMoney >= money_down)
        {
            printf("%s\t%s\t%.2lf",node->usrName,node->usrPwd,node->usrMoney);
        }
        printf("\n");
        node=node->pnext;
    }
    printf("\n");
    huichejixu();
    releaseUsrList(NULL,list);
}

//彩民排序选择界面
int sortByWhatFace()
{
    while(1)
    {
        system("clear");
        printf("请选择排序的依据:\n");
        printf("1.用户名\n");
        printf("2.余额\n");
        printf("0.不想排了\n");
        int iCho=9;
        if(1 != scanf("%d",&iCho))
        {
            chidaodi;;
        }
        else
        {
            getchar();
        }
        if(iCho!=1&&iCho!=2&&iCho!=0)
        {
            printf("选择错误！两秒后请重新选择!\n");
            sleep(2);
            continue;
        }
        return iCho;
    }
}

//根据彩民排序选择的结果进入相应界面
void switchBySortWhatFace()
{
    int ret = sortByWhatFace();
    switch (ret)
    {
    case 1:
        sortByUsrName();
        break;
    case 2:
        sortByUsrMoney();
        break;
    case 0:
        //退出
        break;
    default:
        break;
    }
}

//找到未开奖的彩票信息节点
lyNode * findNotOpenLyNode(lyList *list)
{
    lyNode *node=NULL;
    if(list == NULL)
    {
        return node;
    }
    node = list->pFirstNode;
    while(node !=NULL)
    {
        if(strcmp(node->openstatus,NOT_OPEN) == 0)
        {
            return node;
        }
        node=node->pnext;
    }
    return node;
}

//发行彩票
void issueLottery()
{
    lyList *list=mkLyList();
    loadLotteryInfoFromFile(LOTTERY_PATH,list);
    lyNode *node=findNotOpenLyNode(list);
    if(node != NULL)
    {
        printf("上一期彩票还未开奖！无法发行新彩票！请等公证员开奖！\n");
        return;
    }
    node=mkLyNode();
    node->issue = list->LLen+1;
    node->sum_num = 0;
    node->sum_mon = 0;
    strcpy(node->openstatus,NOT_OPEN);
    printf("请输入第%d期彩票的单价:\n",node->issue);
    scanf("%lf",&(node->price));
    lotteryInsert(list,node);
    saveLotteryListToFile(LOTTERY_PATH,list);
    printf("第%d期彩票发行成功！\n单价为:%.2lf\n",node->issue,node->price);
    huichejixu();
    releaseLyList(NULL,list);
}

//遍历打印用户信息
void displayUsrInfoList(usrList *list)
{
    usrNode *node=list->pFirstNode;
    printf("用户名\t密码\t余额\n");
    while(node !=NULL)
    {
        printf("%s\t%s\t%.2lf\n",node->usrName,node->usrPwd,node->usrMoney);
        node=node->pnext;
    }
}

//根据账户排序
void sortByUsrName()
{
    usrList *list=mkList();
    loadInfoFromFile(PATH_NAME,list);
	if (NULL == list)
	{
		return;
	}
	usrNode *pre = NULL;
	usrNode *cur = NULL;
	int i=0;
	int j=0;
	for (; i<list->LLen; i++)
	{
		pre = list->pFirstNode;
		cur = pre;
		for (j=0; j<list->LLen-1; j++)
		{
			if (strcmp(cur->usrName, cur->pnext->usrName) == 1)
			{
				if (cur == list->pFirstNode)
				{
					list->pFirstNode = cur->pnext;	
					cur->pnext = cur->pnext->pnext;
					list->pFirstNode->pnext = cur;
					pre = list->pFirstNode;
					continue;
				}
				else
				{
					pre->pnext = cur->pnext;
					cur->pnext = cur->pnext->pnext;	
					pre->pnext->pnext = cur;
					pre = pre->pnext;
					continue;
				}
			}	
			pre = cur;
			cur = cur->pnext;
		}
	}
    displayUsrInfoList(list);
    huichejixu();
    releaseUsrList(NULL,list);
}

//根据余额排序
void sortByUsrMoney()
{
    usrList *list=mkList();
    loadInfoFromFile(PATH_NAME,list);
	if (NULL == list)
	{
		return;
	}
	usrNode *pre = NULL;
	usrNode *cur = NULL;
	int i=0;
	int j=0;
	for (; i<list->LLen; i++)
	{
		pre = list->pFirstNode;
		cur = pre;
		for (j=0; j<list->LLen-1; j++)
		{
			if (cur->usrMoney > cur->pnext->usrMoney)
			{
				if (cur == list->pFirstNode)
				{
					list->pFirstNode = cur->pnext;	
					cur->pnext = cur->pnext->pnext;
					list->pFirstNode->pnext = cur;
					pre = list->pFirstNode;
					continue;
				}
				else
				{
					pre->pnext = cur->pnext;
					cur->pnext = cur->pnext->pnext;	
					pre->pnext->pnext = cur;
					pre = pre->pnext;
					continue;
				}
			}	
			pre = cur;
			cur = cur->pnext;
		}
	}
    displayUsrInfoList(list);
    huichejixu();
    releaseUsrList(NULL,list);
}